package com.zhika.zishi.fragments.channel;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Toast;

import com.zhika.zishi.R;
import com.zhika.zishi.activity.StrategyDetailsActivity;
import com.zhika.zishi.adapter.StrategyAdapter;
import com.zhika.zishi.beans.recommend.RecommendStrategyBean;
import com.zhika.zishi.constant.OthersConstant;
import com.zhika.zishi.customview.LoadListView;
import com.zhika.zishi.customview.OptionsPopView;
import com.zhika.zishi.customview.RefreshableView;
import com.zhika.zishi.interfaces.ChannelPopInterface;
import com.zhika.zishi.util.LogUtil;

import java.util.ArrayList;

/**
 * Created by shi-02 on 2015/4/13.
 */
public class ChannelItemFragment extends Fragment implements ChannelPopInterface {

    private static final String TAG = "ChannelItemFragment";

    private OptionsPopView optionsPopView;

    private Context context;

    private RefreshableView refreshableView;

    private LoadListView listView;

    private View view;

    private ArrayList<RecommendStrategyBean> strategyList;

    private StrategyAdapter strategyAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.channel_fragment_layout, container, false);
        initView();

        return view;
    }

    private void initView() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            strategyList = (ArrayList<RecommendStrategyBean>) bundle.getSerializable(OthersConstant.KEY_1);

            LogUtil.e(TAG, "bundle不是空的" + ";size=" + strategyList.size());

        } else {
            LogUtil.e(TAG, "bundle是空的");
        }

        context = getActivity();
        optionsPopView = (OptionsPopView) view.findViewById(R.id.channel_fragment_options_pop_view);
        optionsPopView.setPopInterface(this);

        refreshableView = (RefreshableView) view.findViewById(R.id.channel_fragment_refresh_view);
        refreshableView.setOnRefreshListener(new RefreshableView.PullToRefreshListener() {
            @Override
            public void onRefresh() {
                refreshListener();
            }
        }, OthersConstant.CHANNEL_REFRESH);

        listView = (LoadListView) view.findViewById(R.id.channel_fragment_list_view);
        strategyAdapter = new StrategyAdapter(context, strategyList);
        listView.setAdapter(strategyAdapter);

        listView.setInterface(new LoadListView.ILoadListener() {
            @Override
            public void onLoad() {
                new OnLoadTask().execute();
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent(context, StrategyDetailsActivity.class);
                startActivity(intent);

            }
        });

        //监听listView的滚动事件，如果排序选项（时间、热度）显示，就隐藏掉
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                optionsPopView.hideOptions();
                LogUtil.e(TAG, "listView的触摸事件:onScrollStateChanged");
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            }
        });

    }

    @Override
    public void heatListener() {
        Toast.makeText(context, "按热度排序", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void timeListener() {
        Toast.makeText(context, "按时间排序", Toast.LENGTH_SHORT).show();
    }

    private void refreshListener() {
        String url = "";

//        asyncHttpClient.get(context,url,new AsyncHttpResponseHandler() {
//            @Override
//            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
//                //从服务端得到的json数据
//                String jsonData = new String(responseBody);
//
//            }
//
//            @Override
//            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
//
//            }
//        });

    }


    /**
     * 加载更多事件的真正任务
     */
    private class OnLoadTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {

            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            //在此数据刷新


            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {

            //加载完成
            listView.loadComplete();

            //通知ListView更新界面
            strategyAdapter.notifyDataSetChanged();
        }
    }
}
